package com.maikang.exammall.entity;

import com.fasterxml.jackson.annotation.*;
import com.maikang.exammall.entity.constant.RepositoryConstant;
import com.maikang.exammall.entity.dto.PriceIntervalDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Range;

import javax.persistence.*;

import java.util.List;
import java.util.Set;

import static com.maikang.exammall.entity.Package.TABLE_NAME;

@Entity
@Table(name = TABLE_NAME, schema = RepositoryConstant.APP_DB_TABLE_SCHEMA, catalog = RepositoryConstant.APP_DB_TABLE_SCHEMA)
@DynamicUpdate
@DynamicInsert
@SuperBuilder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler"})
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper=true, exclude = {"hospital", "packageAttributes", "packageDiseaseRelations", "packageCategoryRelations"})
@ToString(callSuper = true, exclude = {"hospital", "packageAttributes", "packageDiseaseRelations", "packageCategoryRelations"})
@SQLDelete(sql = "update " + RepositoryConstant.APP_DB_TABLE_SCHEMA + "." + TABLE_NAME + " set is_deleted = 1 WHERE id = ?")
@Where(clause = " is_deleted = 0 and is_published = 1")
public class Package extends AuditEntity<Long> {
    public static final String TABLE_NAME = "mkp_package";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "hospital_id", referencedColumnName = "id")
    private Hospital hospital;

    private String name;

    @Range(max = 4, min = 0)
    @ApiModelProperty("套餐目标人群 0-均可 1-男士 2-女未婚 3-女已婚, 4-男士")
    private Integer target;
    @Range(max = 6, min = 0)
    @ApiModelProperty("检测目标高发疾病 1-食物不耐受检测，2-骨关节疾病体检 3-健康防癌体检 4-幽门螺旋杆菌检测 5-甲状腺检测 6-糖尿病检测")
    private Integer disease;
    private Long priceOriginal;
    private Long priceReal;
    private String shareDocument;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double distributableRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double firstSaleRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double secondSaleRate;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private Double manageRate;

    private String avatarUrl;
    private String shareImgUrl;
    private String brief;
    private String comment;
    private String tips;
    @ApiModelProperty("点击立即下单数")
    private Integer orderCount;
    @ApiModelProperty("已成交单数")
    private Integer soldCount;

    /**
     * @JsonIgnore
     * @ManyToMany
     * @JoinTable(name = "mkp_package_category",
     * // joinColumns: 当前对象在中间表中的外键
     * joinColumns = {@JoinColumn(name = "pkg_id", referencedColumnName = "id")},
     * // inverseColumns: 对方对象在中间表中的外键
     * inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")}
     * )
     * private Set<Category> categories = new HashSet<>();
     **/


    @ApiModelProperty(hidden = true)
    @OneToMany(mappedBy = "pkg")
    @JsonIgnore
    private Set<PackageCategoryRelation> packageCategoryRelations;

    @ApiModelProperty(hidden = true)
    @OneToMany(mappedBy = "pkg")
    @JsonIgnore
    private Set<PackageDiseaseRelation> packageDiseaseRelations;

    @ApiModelProperty(hidden = true)
    @OneToMany(mappedBy = "pkg")
    @JsonBackReference
    private List<PackageAttribute> packageAttributes;

    @ApiModelProperty(hidden = true)
    @Transient
    private Set<Long> categoryIds;

    @ApiModelProperty(hidden = true)
    @Transient
    private Set<Long> diseaseIds;

    @ApiModelProperty(hidden = true)
    @Transient
    private PriceIntervalDTO priceIntervalDTO;

    @Override
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public Long getPrimaryKey() {
        return this.id;
    }

    @Override
    public void setPrimaryKey(Long id) {
        this.id = id;
    }
}
